package com.spring.a_ioc;

import com.spring.a_ioc.User;
import com.spring.a_ioc.IUserServices;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @author restyhap
 * @date 2019年03月07日 14:57
 */
public class UserTest {
    public static void main(String[] args){
        User user = new User();
        System.out.println(user );
        
    }

    /**
     * 测试通过spring获取一个类的实例
     */
    @Test
    public void mainTest(){
        String xmlPath = "beans_a_ioc.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
        User user = (User) context.getBean("userId");
        System.out.println(user);
    }


    /***
     *
     */
    @Test
    public void mDemo1(){
//        IUserServices is = new IUserServicesImpl();
        String xmlPath = "beans_a_ioc.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
        IUserServices is = (IUserServices) context.getBean("iuserServicesId");
        is.addUser();
    }


    /***
     *
     */
    @Test
    public void mDemo2(){
//        IUserServices is = new IUserServicesImpl();
        String xmlPath = "beans_a_ioc.xml";
        BeanFactory factory = null;
        Resource resource = new ClassPathResource(xmlPath);
        factory = new XmlBeanFactory(resource);
        IUserServices is = (IUserServices) factory.getBean("iuserServicesId");
        is.addUser();
    }
    
    
    
}
